package com.woniu.exception.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.bean.R;
import com.woniu.exception.bean.TException;
import com.woniu.exception.mapper.TExceptionMapper;
import com.woniu.exception.service.ITExceptionService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellowDocter
 * @since 2022-08-12
 */
@RestController
@RequestMapping("/tException")
public class TExceptionController {


    @Autowired
    private ITExceptionService itExceptionService;

   @Autowired
   private TExceptionMapper tExceptionMapper;

    @ApiOperation(value = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "添加对象",value = "添加数据对象",example = "1")
    })
    @CrossOrigin
    @PostMapping
    public R add(@RequestBody  TException tException) {
        /*public R add(@RequestBody TException tException) {*/
        System.out.println("TExceptionController.add");
        System.out.println("拿到了tException = " + tException);

        boolean save = itExceptionService.save(tException);

        R r=null;
        if(save){
            r=R.OK();
        } else {
            r=R.ERROR();
        }
        return r;
    }


    @CrossOrigin
    @PutMapping
    public  R updateCms(@RequestBody  TException tException) {
        /*public R updateCms(@RequestBody TException tException) {*/

        /*System.out.println("tException = " + tException);
        boolean b = itExceptionService.updateById(tException);
        R r=null;
        if(b){
            r=R.OK();
        } else {
            r=R.ERROR();
        }
        return r;*/
        System.out.println("修改方法，前端的数据传过来啦 " + tException);

        QueryWrapper<TException> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("track_Id",tException.getTrackId());

        boolean update = itExceptionService.update(tException, queryWrapper);

        R r=null;
        if(update){
            r=R.OK();
        } else {
            r=R.ERROR();
        }
        return r;
    }

    @CrossOrigin
    @GetMapping("{trackId}")
    public TException  selectById(@PathVariable Integer trackId) {

        System.out.println("trackId = " + trackId);
        System.out.println("前端数据传进来了");

        QueryWrapper<TException> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("track_Id",trackId);

       /* queryWrapper.ge();*/

        TException tException = itExceptionService.getOne(queryWrapper);

        /*  itExceptionService.getById(trackId);*/
       /* if(tException != null){
            updateCms(tException);
        }else {
            tException.setTrackId(trackId);
            add(tException);
        }*/
       return tException;
    }


   /* @RequestMapping(value = "/patientdetails",
            method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public @ResponseBody List<PatientProfileDto> getPatientDetails(
            PatientProfileDto name) {


        List<PatientProfileDto> list = new ArrayList<PatientProfileDto>();
        list = service.getPatient(name);
        return list;
    }*/

}
